//
//  SRSleep+Cate.m
//  SportRing
//
//  Created by Apple on 14/10/27.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRSleep+Cate.h"
#import "SRDB.h"

@implementation SRSleep (Cate)

+ (SRSleep *)sleep
{
    __block SRSleep * sleep = nil;
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         sleep = [SRSleep MR_createEntity];
     }];
    return sleep;
}

- (void)configAttributesWithBlock:(void(^)(SRSleep * aSelf))block
{
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         block(self);
     }];
}

@end

@implementation SRSleep (Find)

+ (NSArray *)findAllUnuploadSleep:(SRMe *)me
{
    __block NSMutableArray * mArray = [@[] mutableCopy];
    [me.days enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        SRDay * day = obj;
        if (day.sleep != nil && day.sleep.isSynced.boolValue == NO) {
            [mArray addObject:day.sleep];
        }
    }];
    return mArray;
}

+ (SRSleep *)findSleepForDay:(SRDay *)day
{
    return day.sleep;
}

+ (SRSleep *)findSleepCINEForDay:(SRDay *)day
{
    SRSleep * sleep = [SRSleep findSleepForDay:day];
    if (sleep == nil) {
        sleep = [SRSleep sleep];
        [sleep configAttributesWithBlock:^(SRSleep *aSelf) {
            aSelf.day = day;
        }];
    }
    return sleep;
}

+ (int)minutesOfDeepSleepForDay:(SRDay *)day
{
    __block int minutes = 0;
    NSArray * sleepParts = [day.sleep.sleepParts.allObjects sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        SRSleepPart * p1 = obj1;
        SRSleepPart * p2 = obj2;
        return [p1.time compare:p2.time];
    }];
    
    if (!sleepParts || [sleepParts count] <2)
    {
        return 0;
    }
    for (int index = 0; index < sleepParts.count - 1; index ++) {
        SRSleepPart * part0 = sleepParts[index];
        SRSleepPart * part1 = sleepParts[index + 1];
        int interval = (part1.time.timeIntervalSince1970 - part0.time.timeIntervalSince1970) / 60;
        if (interval  >= 20) {
            minutes += interval;
        }
    }
    return minutes;
}

+ (int)minutesOfLightSleepForDay:(SRDay *)day
{
    __block int minutes = 0;
    NSArray * sleepParts = [day.sleep.sleepParts.allObjects sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        SRSleepPart * p1 = obj1;
        SRSleepPart * p2 = obj2;
        return [p1.time compare:p2.time];
    }];
    
    if (!sleepParts || [sleepParts count] <2)
    {
        return 0;
    }
    
    for (int index = 0; index < sleepParts.count - 1; index ++) {
        SRSleepPart * part0 = sleepParts[index];
        SRSleepPart * part1 = sleepParts[index + 1];
        int interval = (part1.time.timeIntervalSince1970 - part0.time.timeIntervalSince1970) / 60;
        if (interval  < 20 && interval >= 10) {
            minutes += interval;
        }
    }
    
    return minutes;
}

+ (NSArray *)getSortedArrayWithArray:(NSArray *)array {
    
    return [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        
        NSDate *time0 = [NSDate dateFromString:(NSString *)obj1[@"time"] format:@"HH:mm:ss"];
        NSDate *time1 = [NSDate dateFromString:(NSString *)obj2[@"time"] format:@"HH:mm:ss"];
        return [time0 compare:time1];
    }];
}

//浅水
+ (int)minutesOfLightSleepForArray:(NSArray *)array {
    
    int minutes = 0;
    //    NSArray *sleepParts = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    //        NSDate *time0 = [NSDate dateFromString:(NSString *)obj1[@"time"] format:@"HH:mm:ss"];
    //        NSDate *time1 = [NSDate dateFromString:(NSString *)obj2[@"time"] format:@"HH:mm:ss"];
    //        return [time0 compare:time1];
    //    }];
    
    if (!array || [array count] <2) {
        return 0;
    }
    for (int index = 0; index < array.count - 1; index ++) {
        NSDate *time0 = [NSDate dateFromString:(NSString *)array[index][@"time"] format:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *time1 = [NSDate dateFromString:(NSString *)array[index +1][@"time"] format:@"yyyy-MM-dd HH:mm:ss"];
        int interval = (time1.timeIntervalSince1970 - time0.timeIntervalSince1970)/60;
        if (interval  < 20 && interval >= 10) {
            minutes += interval;
        }
    }
    return minutes;
}
//深睡
+ (int)minutesOfDeepSleepForArray:(NSArray *)array {
    
    int minutes = 0;
//    NSArray *sleepParts = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
//        NSDate *time0 = [NSDate dateFromString:(NSString *)obj1[@"time"] format:@"HH:mm:ss"];
//        NSDate *time1 = [NSDate dateFromString:(NSString *)obj2[@"time"] format:@"HH:mm:ss"];
//        return [time0 compare:time1];
//    }];
    
    if (!array || [array count] <2) {
        return 0;
    }
    for (int index = 0; index < array.count - 1; index ++) {
        NSDate *time0 = [NSDate dateFromString:(NSString *)array[index][@"time"] format:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *time1 = [NSDate dateFromString:(NSString *)array[index +1][@"time"] format:@"yyyy-MM-dd HH:mm:ss"];
        int interval = (time1.timeIntervalSince1970 - time0.timeIntervalSince1970)/60;
        if (interval >= 20) {
            minutes += interval;
        }
    }
    return minutes;
}
//活动
+ (int)minutesOfActiveSleepForArray:(NSArray *)array {
    
    int minutes = 0;
    //    NSArray *sleepParts = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    //        NSDate *time0 = [NSDate dateFromString:(NSString *)obj1[@"time"] format:@"HH:mm:ss"];
    //        NSDate *time1 = [NSDate dateFromString:(NSString *)obj2[@"time"] format:@"HH:mm:ss"];
    //        return [time0 compare:time1];
    //    }];
    
    if (!array || [array count] <2) {
        return 0;
    }
    for (int index = 0; index < array.count - 1; index ++) {
        NSDate *time0 = [NSDate dateFromString:(NSString *)array[index][@"time"] format:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *time1 = [NSDate dateFromString:(NSString *)array[index +1][@"time"] format:@"yyyy-MM-dd HH:mm:ss"];
        int interval = (time1.timeIntervalSince1970 - time0.timeIntervalSince1970)/60;
        if (interval < 10) {
            minutes += interval;
        }
    }
    
    return minutes;
}

+ (int)minutesOfActiveSleepForDay:(SRDay *)day
{
    __block int minutes = 0;
    NSArray * sleepParts = [day.sleep.sleepParts.allObjects sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        SRSleepPart * p1 = obj1;
        SRSleepPart * p2 = obj2;
        return [p1.time compare:p2.time];
    }];
    
    if (!sleepParts || [sleepParts count] <2)
    {
        return 0;
    }
    
    for (int index = 0; index < sleepParts.count - 1; index ++) {
        SRSleepPart * part0 = sleepParts[index];
        SRSleepPart * part1 = sleepParts[index + 1];
        int interval = (part1.time.timeIntervalSince1970 - part0.time.timeIntervalSince1970) / 60;
        if (interval < 10) {
            minutes += interval;
        }
    }
    
    return minutes;
}

+ (int)sleepLevelForDay:(SRDay *)day
{
    int deepMinutes = [SRSleep minutesOfDeepSleepForDay:day];
    if (deepMinutes >= 120) {
        return 5;
    } else if (deepMinutes >= 96) {
        return 4;
    } else if (deepMinutes >= 72) {
        return 3;
    } else if (deepMinutes >= 48) {
        return 2;
    } else if (deepMinutes >= 24) {
        return 1;
    }
    return 0;
}

@end

@implementation SRSleep (Delete)

+ (void)deleteSleepForDay:(SRDay *)day
{
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         [day.sleep MR_deleteEntity];
     }];
}

@end